package org.bit.lesson02;

/**
 * 分别用串行和并行执行两轮10亿次累加
 */
public class Demo201 {
//    private static long count = 10_0000_0000l;

    private static long count = 10_000l;

    public static void main(String[] args) throws InterruptedException {
        // 串行
        serial();
        // 并行
        concurrency();
    }

    private static void concurrency() throws InterruptedException {
        long start = System.currentTimeMillis();

        Thread t1 = new Thread(() -> {
            long a = 0l;
            for (int i = 0; i < count; i++) {
                a++;
            }
        });
        t1.start();

        Thread t2 = new Thread(() -> {
            long b = 0l;
            for (int i = 0; i < count; i++) {
                b++;
            }
        });
        t2.start();

        // 等待 t1 和 t2 执行完
        t1.join();
        t2.join();

        long end = System.currentTimeMillis();
        System.out.println("并行执行时间：" + (end - start));
    }

    private static void serial() {
        long start = System.currentTimeMillis();
        long a = 0l;
        for (int i = 0; i < count; i++) {
            a++;
        }
        long b = 0l;
        for (int i = 0; i < count; i++) {
            b++;
        }
        long end = System.currentTimeMillis();
        System.out.println("串行执行时间：" + (end - start));
    }
}
